package com.tl.khadmin.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.tl.khadmin.bean.DeliveryAddress; 
import com.tl.khadmin.page.Page;
import com.tl.khadmin.service.DeliveryAddressIService; 
import com.tl.khadmin.util.CommonUtil;

/**
 * 发货/收货地址控制器
 * @author 123 
 */
@RequestMapping(value = "/deliveryaddress")
@RestController
public class DeliveryAddressController {
	
	@Resource
	private DeliveryAddressIService deliveryAddressService; 
	
	@RequestMapping(value="/list") 
	public Map<String, List<DeliveryAddress>> listDeliveryAddress(HttpSession session, @RequestParam Map<String ,Object> param){
		return CommonUtil.returnResultCodeJson("deliveryAddress", deliveryAddressService.listDeliveryAddress(session, param));
	}

	@RequestMapping(value = "/showbypage")
	public Map<String, Page<DeliveryAddress>> showProductBrandByPage(HttpSession session, @RequestParam Map<String, Object> params) { 
		Integer pageNum = Integer.parseInt(params.get("pageNum").toString());
		Integer pageSize = Integer.parseInt(params.get("pageSize").toString());
		return CommonUtil.returnResultCodeJson("result", deliveryAddressService.showDeliveryAddressByPage(session, pageNum, pageSize, params));
	}
	
	@RequestMapping(value = "/save")
	@ResponseBody
	public String saveDeliveryAddress(HttpSession session, @Valid DeliveryAddress deliveryAddress, BindingResult br) {
		return deliveryAddressService.saveDeliveryAddress(session, deliveryAddress);
	}
	
	@RequestMapping(value = "/update")
	@ResponseBody
	public String updateDeliveryAddress( @Valid DeliveryAddress deliveryAddress, BindingResult br) {
		return deliveryAddressService.updateDeliveryAddress(deliveryAddress);
	}
	
	@RequestMapping(value = "/updatestatus")
	@ResponseBody
	public String updateStatus(DeliveryAddress deliveryAddress) {
		return deliveryAddressService.updateStatus(deliveryAddress);
	}

	@RequestMapping(value = "/deletebyids")
	@ResponseBody
	public String deleteByIds(String ids) {
		if(StringUtils.isBlank(ids)){
			return CommonUtil.returnMapResultByRows(0, "","未勾选");
		}
		return deliveryAddressService.deleteByIds(ids);
	}

	@RequestMapping(value = "/querybyid")
	@ResponseBody
	public Map<String, Object> queryDeliveryAddressById(Integer id) {
		if(id==null){
			return null;
		}
		return deliveryAddressService.queryDeliveryAddressById(id);
	}
}
